import java.io.*;
import java.util.StringTokenizer;

public class 疯狂的采药 {
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    public static int t,m;
    public static int MAXM = 10001;
    public static int MAXT = 10000001;

    public static int[] time = new int[MAXM];
    public static int[] value = new int[MAXM];
//    public static int[][] dp = new int[MAXM][MAXT];
    public static long[] dp = new long[MAXT];

    public static void main(String[] args) throws IOException {
        t = in.nextInt();
        m = in.nextInt();
        for (int i = 1; i <= m; i++) {
            time[i] = in.nextInt();
            value[i] = in.nextInt();
        }
        out.print(compute1());
        out.close();
    }

//    private static int compute() {
//        for (int i = 1; i <= m; i++) {
//            for (int j = 0; j <= t; j++) {
//                dp[i][j] = dp[i - 1][j];
//                dp[i][j] = Math.max(dp[i][j],dp[i][j - time[i]] + value[i]);
//            }
//        }
//        return dp[m][t];
//    }
    private static long compute1() {
        for (int i = 1; i <= m; i++) {
            for (int j = time[i]; j <= t; j++) {
                dp[j] = Math.max(dp[j],dp[j - time[i]] + value[i]);
            }
        }
        return dp[t];
    }


    public static class Read {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String next() throws IOException {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
    }
}
